#include "subdirectories_queue.h"

#include <stdlib.h>
#include <dirent.h>
#include <string.h>
#include <sys/queue.h>

struct subdirectory {
  char *directory;
  STAILQ_ENTRY(subdirectory) entries;
};

static STAILQ_HEAD(queue_head, subdirectory) head = STAILQ_HEAD_INITIALIZER(head);

void initialize_subdirectories_queue(void)
{
  STAILQ_INIT(&head);
}

void add_subdirectory(const char * const directory_path, size_t length)
{
  struct subdirectory *subdirectory = malloc(sizeof (*subdirectory));
  subdirectory->directory = malloc(length);
  strcpy(subdirectory->directory, directory_path);
  STAILQ_INSERT_HEAD(&head, subdirectory, entries);
}

char *take_subdirectory()
{
  struct subdirectory *subdirectory = STAILQ_FIRST(&head);
  char *directory_path = subdirectory->directory;
  STAILQ_REMOVE_HEAD(&head, entries);
  free(subdirectory);

  return directory_path;
}

bool more_subdirectories()
{
  return !STAILQ_EMPTY(&head);
}
